package com.rockchips.mediacenter.view;


import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import momo.cn.edu.fjnu.androidutils.data.CommonValues;
import momo.cn.edu.fjnu.androidutils.utils.DeviceInfoUtils;
import momo.cn.edu.fjnu.androidutils.utils.SizeUtils;

import android.app.Service;
import android.content.Context;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.RelativeLayout;
import android.widget.VideoView;
import android.app.Activity;

import com.rockchips.mediacenter.R;
import com.rockchips.mediacenter.utils.IICLOG;
import com.rockchips.mediacenter.utils.PlatformUtils;
import com.rockchips.mediacenter.service.IMediaPlayerAdapter;
import com.rockchips.mediacenter.service.IVideoViewAdapter;
import com.rockchips.mediacenter.bean.AudioInfoOfVideo;
import com.rockchips.mediacenter.bean.SubInfo;
import com.rockchips.mediacenter.service.OnBufferingUpdateListener;
import com.rockchips.mediacenter.service.OnCompleteListener;
import com.rockchips.mediacenter.service.OnErrorListener;
import com.rockchips.mediacenter.service.OnFastBackwordCompleteListener;
import com.rockchips.mediacenter.service.OnFastForwardCompleteListener;
import com.rockchips.mediacenter.service.OnInfoListener;
import com.rockchips.mediacenter.service.OnPreparedListener;
import com.rockchips.mediacenter.service.OnSeekCompleteListener;
import com.rockchips.mediacenter.data.ConstData;

import android.graphics.Color;
import android.view.View;
import android.transition.TransitionManager;

public class OrigVideoView extends RelativeLayout implements IVideoViewAdapter{
    private static final String TAG = "OrigVideoView";
    private IICLOG Log = IICLOG.getInstance();

    private MediaPlayer mMediaPlayer = null;
    //private Paint mTextPaint;
    OnBufferingUpdateListener onBufferingUpdateListener = null;
    OnErrorListener onErrorListener = null;
    OnCompleteListener onCompleteListener = null;

    private int maxWidth;
    private int maxHeight;
    private int videoWidth;
    private int videoHeight;
    /**
     * Whether we adjust our view bounds or we fill the remaining area with black bars
     */
    private boolean mAdjustViewBounds;

    /**
     * mCurrentState is a VideoView object's current state.
     */
    private static final int STATE_ERROR = -1;
    private static final int STATE_IDLE = 0;
    private static final int STATE_PREPARING = 1;
    private static final int STATE_PREPARED = 2;
    private static final int STATE_PLAYING = 3;
    private static final int STATE_PAUSED = 4;
    private static final int STATE_PLAYBACK_COMPLETED = 5;

    private int mCurrentState = STATE_IDLE;

    /**
     * Shows the video playback.
     */
    private VideoView mVideoView;

    /**
     * OrigVideoViewListener for VideoPlayerActivity to control video
     */
    private OrigVideoViewListener mOrigVideoViewListener;

    /*** context of this video view*/
    private Context mContext = null;

    /***video view init */
    public OrigVideoView(Context context) {
        this(context, null);
    }

    public OrigVideoView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public OrigVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Log.i(TAG, "OrigVideoView->init()");
        //setOnErrorListener(mOnErrorListener);
        mContext = context;
        inflate(context, R.layout.view_movie, this);
        mVideoView = findViewById(R.id.videoview);
        mVideoView.setOnErrorListener(mOnErrorListener);
//        mVideoView.getHolder().addCallback(new SurfaceHolder.Callback(){
//            @Override
//            public void surfaceDestroyed(SurfaceHolder holder) {
//                Log.d(TAG, "mVideoView surface --> onDestroyed");
//            }
//
//            @Override
//            public void surfaceCreated(SurfaceHolder holder) {
//                Log.d(TAG, "mVideoView surface --> onCreated");
//            }
//
//            @Override
//            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//                Log.d(TAG, "mVideoView surface --> surfaceChanged");
//            }
//
//        });

        if (mMediaplayerAadapter == null) {
            mMediaplayerAadapter = new OrigMediaPlayerAdapter(context);
        }
    }

    public void setAdjustViewBounds(boolean adjustViewBounds) {
        if (mAdjustViewBounds == adjustViewBounds) {
            return;
        }
        mAdjustViewBounds = adjustViewBounds;
        if (adjustViewBounds) {
            setBackground(null);
        } else {
            setBackgroundColor(Color.BLACK);
        }
        requestLayout();
    }

//    @Override
//    public void layout(int l, int t, int r, int b){
//        int left = (maxWidth - videoOrigWidth) / 2;
//        int top = (maxHeight - videoOrigHeight) / 2;
//        boolean isInPictureMode = ((Activity)mContext).isInPictureInPictureMode();
//        if(!isInPictureMode)
//            super.layout(left, top, left + videoOrigWidth, top + videoOrigHeight);
//        else
//            super.layout(l, t, r, b);
//    }

    private int mScreenMode;

    public void setPlayStateInfo(int pp){
            mScreenMode = pp;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mMediaPlayer != null) {
//            final int videoWidth = mMediaPlayer.getVideoWidth();
//            final int videoHeight = mMediaPlayer.getVideoHeight();
            Log.i(TAG, "onMeasure->videoWidth:" + videoWidth);
            Log.i(TAG, "onMeasure->videoHeight:" + videoHeight);
            if (videoWidth != 0 && videoHeight != 0) {
                final float aspectRatio = (float) videoHeight / videoWidth;
                final int width = MeasureSpec.getSize(widthMeasureSpec);
                final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
                final int height = MeasureSpec.getSize(heightMeasureSpec);
                final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
                Log.i(TAG, "onMeasure->mAdjustViewBounds:" + mAdjustViewBounds);
                Log.i(TAG, "onMeasure->width:" + width);
                Log.i(TAG, "onMeasure->height:" + height);
                Log.d(TAG, "mPlayStateInfo.getScreenMode() is " + mScreenMode);
                if (mAdjustViewBounds) {
                    if (widthMode == MeasureSpec.EXACTLY && heightMode != MeasureSpec.EXACTLY) {
                        super.onMeasure(
                                widthMeasureSpec,
                                MeasureSpec.makeMeasureSpec(
                                        (int) (width * aspectRatio), MeasureSpec.EXACTLY));
                    } else if (widthMode != MeasureSpec.EXACTLY
                            && heightMode == MeasureSpec.EXACTLY) {
                        super.onMeasure(
                                MeasureSpec.makeMeasureSpec(
                                        (int) (height / aspectRatio), MeasureSpec.EXACTLY),
                                heightMeasureSpec);
                    } else {
                        super.onMeasure(
                                widthMeasureSpec,
                                MeasureSpec.makeMeasureSpec(
                                        (int) (width * aspectRatio), MeasureSpec.EXACTLY));
                    }
                } else {
                    final float viewRatio = (float) height / width;
                    Log.d(TAG, "widthMeasureSpec " + widthMeasureSpec + ",heightMeasureSpec " + heightMeasureSpec);
                    if(mScreenMode == ConstData.ScreenMode.SCREEN_FULL) {
                        Log.d(TAG, "ConstData.ScreenMode.SCREEN_FULL ");
                        setPadding(0, 0, 0, 0);
                        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                        return;
                    }
                    if(mScreenMode == ConstData.ScreenMode.SCREEN_ORIGINAL) {
                        Log.d(TAG, "ConstData.ScreenMode.SCREEN_ORIGINAL");
                        if(videoWidth > width || videoHeight > height){

                            if(viewRatio > aspectRatio) {

                                //outHeight = videoHeight *  width / videoWidth;
                                //outWidth = width;
                                int padding = (height - videoHeight *  width / videoWidth) / 2;
                                Log.d(TAG, "ConstData.ScreenMode.SCREEN_ORIGINAL---1 " + padding);
                                setPadding(0, padding, 0, padding);
                            }else{
                                //outWidth = videoWidth * height / videoHeight;
                                //outHeight = height;
                                int padding = (width - videoWidth *  height / videoHeight) / 2;
                                Log.d(TAG, "ConstData.ScreenMode.SCREEN_ORIGINAL---2 " + padding);
                                setPadding(padding, 0, padding, 0);
                            }
                        }else{
                            int padding_width = (width - videoWidth) / 2;
                            int padding_height = (height - videoHeight) / 2;
                            Log.d(TAG, "ConstData.ScreenMode.SCREEN_ORIGINAL---3 " + padding_width  + "," + padding_height);
                            setPadding(padding_width, padding_height, padding_width, padding_height);
                        }
                        //Log.d(TAG, "ConstData.ScreenMode.SCREEN_ORIGINAL outWidth : " + outWidth + " outHeight : " +outHeight);
                        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                        return;
                    }

                    if (aspectRatio > viewRatio) {
                        int padding = (int) ((width - height / aspectRatio) / 2);
                        setPadding(padding, 0, padding, 0);
                    } else {
                        int padding = (int) ((height - width * aspectRatio) / 2);
                        setPadding(0, padding, 0, padding);
                    }
                    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                }
                return;
            }
        } else {
            //Log.e(TAG, "mMediaPlayer is null, onMeasure error");
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    /**
     * Monitors all events related to {@link MovieView}.
     */
    public abstract static class OrigVideoViewListener {

        /**
         * Called when the video is started or resumed.
         */
        public void onVideoStarted() {
        }

        /**
         * Called when the video is paused or finished.
         */
        public void onVideoStopped() {
        }

        /**
         * Called when this view should be minimized.
         */
        public void onVideoMinimized() {
        }
    }

    /**
     * Sets the listener to monitor movie events.
     *
     * @param movieListener The listener to be set.
     */
    public void setOrigVideoViewListener(OrigVideoViewListener origVideoViewListener) {
        mOrigVideoViewListener = origVideoViewListener;
    }

    /****************video operations of start, pause, seek , stop, setDataSource **********************************/

    /***defined for abstrack medthod*/
    public void setVideoPath(String path) {
    }

    public void start(){
        Log.i(TAG, "start()");
        mVideoView.start();
        if(mOrigVideoViewListener != null){
            mOrigVideoViewListener.onVideoStarted();
        }
    };
    public void setSubSurfaceHolder(SurfaceHolder m){}

    /***defined for abstrack medthod*/
    public void setOutRange(int left, int top, int w, int h) {

        Log.d(TAG, "setOutRange begin ");
        if(!isReady())
        {
            Log.e(TAG, "setOutRange isReady fasle!");
            return;
        }
        videoWidth= w;
        videoHeight = h;

        this.requestLayout();
    }

    /***defined for abstrack medthod*/
//    public void stopPlayback() {
//        if (mMediaPlayer != null) {
//            mMediaPlayer.stop();
//            mMediaPlayer.release();
//            mMediaPlayer = null;
//        }else{
//            Log.e(TAG, "mMediaPlayer is null ,stopPlayback error");
//        }
//    }

    public int getDuration() {
        if (mMediaPlayer != null) {
            return mMediaPlayer.getDuration();
        }
        return -1;
    }

    public boolean isPlaying() {
        try{
        return mMediaPlayer != null && mMediaPlayer.isPlaying();
        }catch(Exception e){
            return false;
        }
    }
//
    public void seekTo(int msec) {
        if (mVideoView != null) {
            mVideoView.seekTo(msec);
        } else {
            Log.e(TAG, "mMediaPlayer is null ,seekTo error");
        }
    }

    /**
     * Returns the current position of the video. If the the player has not been created, then
     * assumes the beginning of the video.
     *
     * @return The current position of the video.
     */
    public int getCurrentPosition() {
        if (mMediaPlayer == null) {
            Log.i(TAG, "getCurrentPosition mMediaPlayer is null, return 0");
            return 0;
        }
        int result = 0;
        try{
            result = mMediaPlayer.getCurrentPosition();
        }catch (Exception e){
            Log.i(TAG, "getCurrentPosition mMediaPlayer cause IllegalStateException, return 0");
            result = 0;
        }
        return result;
    }

//    public void play() {
//        Log.i(TAG, "play()");
//        if (mMediaPlayer == null) {
//            return;
//        }
//        mMediaPlayer.start();
//        setKeepScreenOn(true);
//        if (mOrigVideoViewListener != null) {
//            mOrigVideoViewListener.onVideoStarted();
//        }
//    }

    public void pause() {
        mVideoView.pause();
        if(mOrigVideoViewListener != null){
            mOrigVideoViewListener.onVideoStopped();
        }
    }

    @Override
    public void stopPlayback()
    {
        mIsPrepared = false;
        mVideoView.stopPlayback();
        mMediaPlayer = null;
        mMediaplayerAadapter.release();

    }

    public void resume()
    {
        mVideoView.resume();
    }

//    void openVideo() {
//        if (mUri == null || mSurfaceHolder == null) {
//            Log.e(TAG, "openVideo error, mUri == null or mSurfaceHolder == null");
//            return;
//        }
//        if(mMediaPlayer == null)
//            mMediaPlayer = new MediaPlayer();
//        if(mSurface != null && mSurface.isValid()) {
//            mMediaPlayer.setSurface(mSurface);
//        } else {
//            Log.e(TAG, "openVideo error");
//            return;
//        }
//        startVideo();
//    }

//    private Uri getVideoUri() {
//        return mUri;
//    }

    @Override
    public void setVideoURI(Uri uri) {
        mVideoView.setVideoURI(uri);
    }

    /**
     * Restarts playback of the video.
     */
//    public void startVideo() {
//        mMediaPlayer.reset();
//        Uri uri = null;
//        try {
//            uri = getVideoUri();
//            mMediaPlayer.setDataSource(mContext, uri);
//            mMediaPlayer.setOnPreparedListener(mPreparedListener);
//            mMediaPlayer.prepare();
//        } catch (Exception e) {
//            Log.e(TAG, "Failed to open video " + e);
//        }
//    }

//    public void closeVideo() {
//        mIsPrepared = false;
//        if (mMediaPlayer != null) {
//            mMediaPlayer.stop();
//            mMediaPlayer.release();
//            mMediaPlayer = null;
//        }
//        mMediaplayerAadapter.release();
//    }


    /************************************************************************************/

    /**
     * Mender:l00174030;Reason:from android2.2
     **/
    public boolean isSeeking = false;

    public void isSeeking(boolean b) {
        isSeeking = b;
    }

    public boolean isSeeking() {
        return isSeeking;
    }

    private OnInfoListener mOnInfoListener;
    private MediaPlayer.OnInfoListener mInfoListener = new MediaPlayer.OnInfoListener() {
        public boolean onInfo(MediaPlayer mp, int what, int extra) {
            if (mOnInfoListener != null) {
                return mOnInfoListener.onInfo(getmediaPlayerAdapter(), what, extra);
            }
            return true;
        }
    };

    public void setOnInfoListener(OnInfoListener l) {
        mOnInfoListener = l;
        mVideoView.setOnInfoListener(mInfoListener);
    }

    private OnSeekCompleteListener mOnSeekCompleteListener;
    OnSeekCompleteListener mSeekCompleteListener = new OnSeekCompleteListener()
    {

        public void onSeekComplete(IMediaPlayerAdapter mp)
        {
            Log.e(TAG, "seekTo  is complete---->" + isSeeking);
            if (mOnSeekCompleteListener != null)
            {
                mOnSeekCompleteListener.onSeekComplete(mp);
            }
            isSeeking(false);
            Log.e(TAG, "seekTo  is complete  posistion---->" + getCurrentPosition());
        }
    };

    private MediaPlayer.OnSeekCompleteListener mseekListener = new MediaPlayer.OnSeekCompleteListener() {
        public void onSeekComplete(MediaPlayer arg0) {
            if (mOnSeekCompleteListener != null) {
                mOnSeekCompleteListener.onSeekComplete(getmediaPlayerAdapter());
            }
        }
    };

    public void setOnSeekCompleteListener(OnSeekCompleteListener l) {
        mOnSeekCompleteListener = l;
    }

    private OnCompleteListener mOnCompleteListener;
    private MediaPlayer.OnCompletionListener mcompleteListener = new MediaPlayer.OnCompletionListener() {

        public void onCompletion(MediaPlayer arg0) {
            Log.i(TAG, "onCompletion");
            if (mOnCompleteListener != null) {
                mOnCompleteListener.onCompletion(getmediaPlayerAdapter());
                if (mOrigVideoViewListener != null) {
                    mOrigVideoViewListener.onVideoStopped();
                }
            }else{
                Log.e(TAG, "mOnCompleteListener is null");
            }
        }
    };

    public void setOnCompletionListener(OnCompleteListener l) {
        mOnCompleteListener = l;
    }

    private List<AudioInfoOfVideo> audioinfos = new ArrayList<AudioInfoOfVideo>();
    private Map<String, Integer> audioTrackMap = new HashMap<String, Integer>();
    private List<SubInfo> subinfos = new ArrayList<SubInfo>();

    private boolean mIsPrepared = false;
    private OnPreparedListener mCustomPrepareListener = null;

    private OrigMediaPlayerAdapter mMediaplayerAadapter = null;
    MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {
        public void onPrepared(MediaPlayer mp) {
            Log.i(TAG, "OrigVideoView->onPrepared");
            mIsPrepared = true;
            mMediaPlayer = mp;
            mMediaplayerAadapter.setMediaPlayer(mp);
            //setmediaPlayerAdapter(mediaplayer);
            mp.setOnBufferingUpdateListener(mbufferingListener);
            mp.setOnErrorListener(mOnErrorListener);
            mp.setOnCompletionListener(mcompleteListener);
            mp.setOnSeekCompleteListener(mseekListener);
            setSubTrackInfo();
            if(getAudioinfos() == null)
            {
                Log.e(TAG, "get Audio Infos failed, return null");
            }
            //if(getAudioinfos().size()>0)
            //{
                //maudioInfoOfVidio = getAudioinfos().get(0);
            //}
            mCustomPrepareListener.onPrepared(getmediaPlayerAdapter());
            //setSoundId(0);

        }
    };

    public void setOnPreparedListener(OnPreparedListener l) {
        mCustomPrepareListener = l;
        mVideoView.setOnPreparedListener(mPreparedListener);
    }

    private boolean isReady() {
        if (mMediaPlayer != null && mIsPrepared) {
            return true;
        }
        return false;
    }

    @Override
    public int getCurrentSoundId() {
        if (!isReady()) {
            return -1;
        }

        return getmediaPlayerAdapter().getCurrentSndId();
    }

    @Override
    public int getCurrentSudId() {
        if (!isReady()) {
            return -1;
        }

        return getmediaPlayerAdapter().getCurrentSubId();
    }

    @Override
    public IMediaPlayerAdapter getmediaPlayerAdapter() {
        return mMediaplayerAadapter;
    }

    @Override
    public OnBufferingUpdateListener getonBufferingUpdateListener() {
        return onBufferingUpdateListener;

    }

    @Override
    public OnErrorListener getonErrorListener() {
        return onErrorListener;
    }

    @Override
    public OnInfoListener getonInfoListener() {
        return mOnInfoListener;
    }

    @Override
    public OnPreparedListener getonPreparedListener() {
        return mCustomPrepareListener;
    }

    @Override
    public OnSeekCompleteListener getonSeekCompleteListener() {
        return mOnSeekCompleteListener;
    }

    @Override
    public List<SubInfo> getSubtitleList() {
        return subinfos;
    }

    @Override
    public int getVideoHeight() {
        if (!isReady()) {
            return -1;
        }
        return getmediaPlayerAdapter().getVideoHeight();
    }

    @Override
    public int getVideoWidth() {
        if (!isReady()) {
            return -1;
        }

        return getmediaPlayerAdapter().getVideoWidth();
    }

    @Override
    public boolean isSubtitleShowing() {
        if (!isReady()) {
            return false;
        }

        return getmediaPlayerAdapter().isSubtitleShowing();
    }


    @Override
    public void setmediaPlayerAdapter(IMediaPlayerAdapter newVal) {
        mMediaplayerAadapter = (OrigMediaPlayerAdapter) newVal;
    }

    private MediaPlayer.OnBufferingUpdateListener mbufferingListener = new MediaPlayer.OnBufferingUpdateListener() {
        @Override
        public void onBufferingUpdate(MediaPlayer arg0, int percent) {
            if (onBufferingUpdateListener != null) {
                onBufferingUpdateListener.onBufferingUpdate(getmediaPlayerAdapter(), percent);
            }
        }
    };

    @Override
    public void setOnBufferingUpdateListener(OnBufferingUpdateListener newVal) {
        onBufferingUpdateListener = newVal;
    }

    @Override
    public void setOnErrorListener(OnErrorListener newVal) {
        onErrorListener = newVal;
    }

    private MediaPlayer.OnErrorListener mOnErrorListener = new MediaPlayer.OnErrorListener() {

        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            if (onErrorListener != null) {
                mMediaplayerAadapter.setMediaPlayer(mp);
                //setmediaPlayerAdapter(mMediaplayerAadapter);
                onErrorListener.onError(getmediaPlayerAdapter(), what, extra);
            }
            return true;
        }
    };

    @Override
    public void setSoundId(int id) {
        if (!isReady()) {
            Log.d(TAG, "setSoundId not Ready  just return !");
            return;
        }

        if (0 > id || id >= audioinfos.size()) {
            Log.e(TAG, "setSoundId invalid id " + id);
            return;
        }
        String language = audioinfos.get(id).getlauguage();
        Integer index = audioTrackMap.get(language);
        Log.d(TAG, "setSoundId id = " + index + ", language = " + language);

        maudioInfoOfVidio = audioinfos.get(id);
        int i = getmediaPlayerAdapter().setSoundId(index);

        Log.d(TAG, "setSoundId return :" + i);
    }

    @Override
    public void setSubId(int id) {
        if (!isReady()) {
            return;
        }

        int i = getmediaPlayerAdapter().setSubId(id);

        Log.d(TAG, "setSubId return :" + i);
    }

    @Override
    public void showSubtitle(boolean show) {
        if (!isReady()) {
            return;
        }

        int flag = show ? 0 : 1;

        int i = getmediaPlayerAdapter().enableSubtitle(flag);

        Log.d(TAG, "showSubtitle return :" + i);

    }

    /**
     * @return 返回 audioinfos
     */
    public List<AudioInfoOfVideo> getAudioinfos() {
        return audioinfos;
    }

    private AudioInfoOfVideo maudioInfoOfVidio = null;

    public AudioInfoOfVideo getCurrentAudioinfos() {
        return maudioInfoOfVidio;
    }

    /**
     * 设置ｓｕｒｆａｃｅ宽高
     * <功能详细描述>
     *
     * @param w
     * @param h
     * @return 成功　返回０；否则失败
     * @see [类、类#方法、类#成员]
     */
    public int setScreenScale(int w, int h) {
        if (!isReady()) {
            return -1;
        }

        return mMediaplayerAadapter.setScreenScale(w, h);
    }

    private OnFastForwardCompleteListener mOnFastForwardCompleteListener = null;
    private OnFastBackwordCompleteListener mOnFastBackwordCompleteListener = null;

    @Override
    public void setOnFastForwardCompleteListener(OnFastForwardCompleteListener l) {
        mOnFastForwardCompleteListener = l;
    }

    @Override
    public void setOnBackForwardCompleteListener(OnFastBackwordCompleteListener l) {
        mOnFastBackwordCompleteListener = l;
    }

    public int setSpeed(int i) {
        if (!isReady()) {
            return -1;
        }

        return mMediaplayerAadapter.setSpeed(i);
    }

    @Override
    public boolean setAudioChannelMode(int channelMode) {
        return getmediaPlayerAdapter().setAudioChannelMode(channelMode);
    }

    @Override
    public int setSubPath(String path) {
        return getmediaPlayerAdapter().setSubPath(path);
    }

    @Override
    public int getBufferSizeStatus() {
        // TODO Auto-generated method stub
        if (!isReady()) {
            return -1;
        }
        return mMediaplayerAadapter.getBufferSizeStatus();
    }

    @Override
    public int getBufferTimeStatus() {
        // TODO Auto-generated method stub
        if (!isReady()) {
            return -1;
        }
        return mMediaplayerAadapter.getBufferTimeStatus();
    }

    private void setSubTrackInfo() {
        if (mMediaPlayer != null) {
            audioinfos.clear();
            audioTrackMap.clear();
            subinfos.clear();
            //DTS2015012804581 by wWX170514  根据caochao问题单中的描述，调用接口函数被人修改，导致问题的出现
            //MediaPlayer.TrackInfo[] trackInfo = mMediaPlayer.getAudioTrack();

            MediaPlayer.TrackInfo[] trackInfo ;
            try {
                trackInfo = mMediaPlayer.getTrackInfo();
            }catch (Exception exception){
                Log.e(TAG,"catch getTrackInfo exception:" + exception);
                return;
            }
            int trackType = -1;
            int s = 1;
            String language = null;
            //int subTitleId = 0;
            Log.d(TAG, "trackInfo.length = " + trackInfo.length);
            for (int i = 0; i < trackInfo.length; i++) {
                if(trackInfo[i] == null){
                    Log.e(TAG, "error! trackInfo[ " + i + "] is null!");
                    continue;
                }
                trackType = trackInfo[i].getTrackType();
                language = trackInfo[i].getLanguage();
                Log.d(TAG, " trackType = " + trackType);
                if (trackType == MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_AUDIO) {
                    if ("und".equals(language)) {
                        language = "音轨" + s;
                        s++;
                    }
                    Log.d(TAG, " sound id = " + i + " language = " + language);
                    AudioInfoOfVideo audioInfo = new AudioInfoOfVideo();
                    audioInfo.setlauguage(language);
                    audioinfos.add(audioInfo);
                    audioTrackMap.put(language, i);
                }
                //内置字幕
                else if (trackType == MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT) {
                    //这里获取的是字幕信息
                    SubInfo subInfo = new SubInfo();
                    subInfo.setLanguage(trackInfo[i].getLanguage());
                    subInfo.setFormat(trackInfo[i].getFormat().toString());
                    subInfo.setSubid(i);
                    subInfo.setIsExtra(false);
                    //trackInfo[i].getLanguage();
                    subinfos.add(subInfo);
                }
                //外挂字幕
                else if (trackType == MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE) {
                    //这里获取的是字幕信息
                    SubInfo subInfo = new SubInfo();
                    subInfo.setLanguage(trackInfo[i].getLanguage());
                    subInfo.setFormat(trackInfo[i].getFormat().toString());
                    subInfo.setSubid(i);
                    subInfo.setIsExtra(true);
                    //trackInfo[i].getLanguage();
                    subinfos.add(subInfo);
                }

            }
        }
    }

    @Override
    public boolean isDolbyEnabled() {
        return getmediaPlayerAdapter().isDolbyEnabled();
    }

    public void setFieldValue(String filedName, Object value) {
        try {
            Field field = getClass().getSuperclass().getDeclaredField(filedName);
            field.setAccessible(true);
            field.set(this, value);
        } catch (Exception e) {
            Log.e(TAG, "setFieldValue->filedName:" + filedName);
            Log.e(TAG, "setFieldValue->value:" + value);
            Log.e(TAG, "setFieldValue->exception:" + e);
        }

    }

    public Object getFieldValue(String filedName) {
        try {
            Field field = getClass().getSuperclass().getDeclaredField(filedName);
            field.setAccessible(true);
            return field.get(this);
        } catch (Exception e) {
            Log.e(TAG, "getFieldValue->filedName:" + filedName);
            Log.e(TAG, "getFieldValue->exception:" + e);
        }
        return null;
    }

    public Object invokeMethod(Object object, String methodName, Class<?>[] paramTypes, Object[] values) {
        try {
            Method method = object.getClass().getDeclaredMethod(methodName, paramTypes);
            method.setAccessible(true);
            return method.invoke(object, values);
            //return method.invoke(object, paramTypes);
        } catch (Exception e) {
            Log.e(TAG, "invokeMethod->methodName:" + methodName);
            Log.e(TAG, "invokeMethod->exception:" + e);

        }
        return null;
    }

}
